/*
 *
 *
Copyright (c) 1998-2001 Aristar, Inc. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. The end-user documentation included with the redistribution, if any,
must include the following acknowledgment:

"This product includes software developed by Aristar, Inc. (http://www.aristar.com/)."

Alternately, this acknowledgment may appear in the software itself, if and
wherever such third-party acknowledgments normally appear.

4. The names "Dialect" and "Aristar, Inc." must not be used to endorse or
promote products derived from this software without prior written permission.
For written permission, please contact info@aristar.com.

5. Products derived from this software may not be called "Dialect", nor may
"Dialect" appear in their name, without prior written permission of Aristar, Inc.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
ARISTAR, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

This software consists of voluntary contributions made by many individuals
on behalf of Aristar, Inc.  For more information on Aristar, Inc.,
please see <http://www.aristar.com/>.

Portions of this software are based upon software freely available and
written by individuals not associated with Aristar, Inc.  Copyrights are
maintained in the relevant source code files.
 *
 *
 */

#include "stdafx.h"
#include "DConfigGlobals.h"
#include "Interpreter.h"
#include "COutputModule.h"
#include "COutputDevice.h"
#include "CMeta.h"

////////////////////////////////////////////////////////////////////

VALUE InitOutputModule()
{
	VALUE symtab = AllocFrame(7);
	symtab.Frame()->Add(_T("__module__"), (LPCTSTR)OUTPUT_MODULE_NAME);
#if !defined(WIN32_PLATFORM_PSPC)
	symtab.Frame()->Add(_T("printer"), AllocClass(new CPrinterOutImpl()));
#endif
	//symtab.Frame()->Add(_T("meta"), AllocClass(new CMetaOutImpl())); // Until scripts change
#ifndef DIALECT_LITE
	symtab.Frame()->Add(_T("screen"), AllocClass(new CMetaOutImpl()));
#endif
	symtab.Frame()->Add(_T("rtf"), AllocClass(new CRTFOutImpl()));
	symtab.Frame()->Add(_T("window"), AllocClass(new CWindowOutImpl()));
	return symtab;
}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

static VALUE drawLine(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 5) {
		pInterp->SyntaxError(_T("drawline requires five args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}
	float x1 = args[0].Float();
	float y1 = args[1].Float();
	float x2 = args[2].Float();
	float y2 = args[3].Float();
	float width = args[4].Float();
	out->DrawLine(x1, y1, x2, y2, width);
	return AllocNil();
}

static VALUE write(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 1) {
		pInterp->SyntaxError(_T("write requires one arg"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->TextOut(args[0].String());
	return AllocNil();
}

static VALUE writeline(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount > 1) {
		pInterp->SyntaxError(_T("writeline requires zero or one arg"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	if (argCount == 1) out->TextOut(args[0].String());
	out->TextOutCR();
	return AllocNil();
}

static VALUE writeTab(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("Tab requires zero args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->TextOutTab();
	return AllocNil();
}

static VALUE writeTable(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if ((argCount != 3) || (args[0].Type() != stArray) || (args[1].Type() != stArray) ||
		(args[2].Type() != stArray)) {
		pInterp->SyntaxError(_T("Table requires three array args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	TTable table(args[0].Array(), args[1].Array(), args[2].Array());
	out->PrintTable(table);
	return AllocNil();
}

static VALUE writeSitem(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 2) {
		pInterp->SyntaxError(_T("SectItem requires two args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->SectionItem(args[0].String(), args[1].String(), '.');
	return AllocNil();
}

static VALUE startSection(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if ((argCount != 3) || (args[2].Type() != stInteger)) {
		pInterp->SyntaxError(_T("startSection requires three args: two strings and one integer"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->StartSection(args[0].String(), args[1].String(), args[2].Integer());
	return AllocNil();
}

static VALUE endSection(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("endsection requires zero args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->EndSection();
	return AllocNil();
}

static VALUE newPage(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("newPage requires zero args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->NewPage();
	return AllocNil();
}

static VALUE startDoc(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if ((argCount != 1) || (args[0].Type() != stString)) {
		pInterp->SyntaxError(_T("startDoc requires one string arg"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->StartDocument(args[0].String());
	return AllocNil();
}

static VALUE endDoc(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("endDoc requires zero args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->EndDocument();
	return AllocNil();
}

static VALUE abortDoc(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("abortDoc requires zero args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->AbortDocument();
	return AllocNil();
}

static VALUE linesLeft(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("lineleft requires zero args"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	return out->LinesLeft();
}

static VALUE textWidth(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 1) {
		pInterp->SyntaxError(_T("textwidth requires one string arg"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}
	float x = out->TextWidth(args[0].String());
	return VALUE(x); // Some strange optimization bug?
}

static VALUE writePageHeaders(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// Write property function

	if (args[1].Type() != stArray) {
		pInterp->SyntaxError(_T("pageHeaders must be an array of strings"));
		return args[0];
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return args[0];
	}

	for (int idx = 1; idx <= args[1].Length(); ++idx) {
		out->SetPageHeader(idx, args[1][idx].String());
	}
	return args[1];
}

static VALUE writeMargins(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// Write property function

	if (args[1].Type() != stArray) {
		pInterp->SyntaxError(_T("margins requires a four element array of floats"));
		return args[0];
	}

	if ((args[1][1].Type() != stFloat) || (args[1][2].Type() != stFloat) ||
		(args[1][3].Type() != stFloat) || (args[1][4].Type() != stFloat)) {
		pInterp->SyntaxError(_T("margins requires a four element array of floats"));
		return args[0];
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return args[0];
	}

	out->SetMargins(args[1].Float(), args[2].Float(), args[3].Float(), args[4].Float());
	return args[1];
}

static VALUE writeWrapIndent(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// Write property function

	if (args[1].Type() != stInteger) {
		pInterp->SyntaxError(_T("wrapindent requires an integer value"));
		return args[0];
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return args[0];
	}

	out->SetWrapIndent(args[1].Integer());
	return args[1];
}

static VALUE readPosition(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// Read property function

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	VALUE vResult = AllocArray(2);
	vResult.Set(1, out->GetX());
	vResult.Set(2, out->GetY());
	return vResult;
}

static VALUE writePosition(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// Write property function

	if ((args[1].Type() != stArray) || (args[1].Length() != 2)) {
		pInterp->SyntaxError(_T("position expects two element array [x, y]"));
		return AllocNil();
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}

	out->SetX(args[1][1].Float());
	out->SetY(args[1][2].Float());
	return args[1];
}

static VALUE writeFont(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// Write property function
	if (args[1].Type() != stFrame) {
		pInterp->SyntaxError(_T("Invalid font"));
		return AllocNil();
	}

	TString fontName = _T("Arial");
	int fontSize = 10;
	U32 fontStyle = 0;
	if (args[1][_T("name")].Type() == stString) {
		fontName = args[1][_T("name")].String();
	}
	if (args[1][_T("size")].Type() == stInteger) {
		fontSize = args[1][_T("size")].Integer() / 10; // For consistancy in Dialect
	}
	if (args[1][_T("bold")].Boolean()) {
		fontStyle |= FN_BOLD;
	}
	if (args[1][_T("italic")].Boolean()) {
		fontStyle |= FN_ITALIC;
	}
	if (args[1][_T("underline")].Boolean()) {
		fontStyle |= FN_UNDERLINE;
	}

	COutputDev* out = (COutputDev*)self->Get(_T("#lpOut")).Void();
	if (out == NULL) {
		pInterp->SyntaxError(_T("No output instance was instantiated"));
		return AllocNil();
	}
	out->SetFont(fontName, fontSize, fontStyle);
	return args[1];
}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

// Called by stVoid garbage collection
static void freeOutputDev(void* pVoid) {
	COutputDev* lpOutputDev = (COutputDev*)pVoid;
	if (lpOutputDev != NULL) {
		delete lpOutputDev;
	}
}

// Called by stVoid garbage collection
// Use this version for windowout and metaout since they
// currently only support one instance, just close doc, not window
static void freeSpecial(void* pVoid) {

}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

#if !defined(WIN32_PLATFORM_PSPC)

CPrinterOutImpl::CPrinterOutImpl():CClassImpl(VALUE(), _T("printer"))
{
	m_MemberSymTab = AllocFrame(19);
	// Anything added here is static (becomes shared by all instances of the class)
	m_MemberSymTab.Set(_T("__type__"), _T("printer"));
	m_MemberSymTab.Set(_T("__init__"), AllocFunction(__init__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("__del__"), AllocFunction(__del__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("write"), AllocFunction(write, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("writeline"), AllocFunction(writeline, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("tab"), AllocFunction(writeTab, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("table"), AllocFunction(writeTable, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("sectitem"), AllocFunction(writeSitem, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startsection"), AllocFunction(startSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("endsection"), AllocFunction(endSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("newpage"), AllocFunction(newPage, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startdoc"), AllocFunction(startDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("enddoc"), AllocFunction(endDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("abortdoc"), AllocFunction(abortDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("linesleft"), AllocFunction(linesLeft, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("textwidth"), AllocFunction(textWidth, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("drawline"), AllocFunction(drawLine, (LPCTSTR)OUTPUT_MODULE_NAME));

	// Set margins to default of 1 inch on all sides
	VALUE vMargins = AllocArray(4);
	vMargins.Set(1, 1.0f);
	vMargins.Set(2, 1.0f);
	vMargins.Set(3, 1.0f);
	vMargins.Set(4, 1.0f);
	m_MemberSymTab.Set(_T("margins"), AllocProperty(AllocNil(),
		AllocFunction(writeMargins, (LPCTSTR)OUTPUT_MODULE_NAME), vMargins));

	m_MemberSymTab.Set(_T("pageheaders"), AllocProperty(AllocNil(),
		AllocFunction(writePageHeaders, (LPCTSTR)OUTPUT_MODULE_NAME), AllocArray(0)));

	m_MemberSymTab.Set(_T("wrapindent"), AllocProperty(AllocNil(),
		AllocFunction(writeWrapIndent, (LPCTSTR)OUTPUT_MODULE_NAME), (int)0));

	m_MemberSymTab.Set(_T("position"),
		AllocProperty(AllocFunction(readPosition, (LPCTSTR)OUTPUT_MODULE_NAME),
		AllocFunction(writePosition, (LPCTSTR)OUTPUT_MODULE_NAME), (int)0));

	m_MemberSymTab.Set(_T("font"), AllocProperty(AllocNil(),
		AllocFunction(writeFont, (LPCTSTR)OUTPUT_MODULE_NAME), AllocNil()));
}

CPrinterOutImpl::~CPrinterOutImpl()
{
}

VALUE CPrinterOutImpl::__init__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if ((argCount == 0) || ((argCount == 1) && (args[0].Type() == stNil))) {
		CPrinterOutputDev* lpOut = new CPrinterOutputDev(NULL);
		self->Set(_T("#lpOut"), AllocVoid(lpOut, freeOutputDev, _T("cprinteroutputdev")));
	}
	else if ((argCount == 1) && (args[0].Type() == stString)) {
		CPrinterOutputDev* lpOut = new CPrinterOutputDev(args[0].String());
		self->Set(_T("#lpOut"), AllocVoid(lpOut, freeOutputDev, _T("cprinteroutputdev")));
	}
	else {
		pInterp->SyntaxError(_T("outputs.printer(<UNCprintName>)"));
		return AllocNil();
	}

	return VALUE();
}

VALUE CPrinterOutImpl::__del__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// There shouldn't be any args for the destructor
	ASSERT(argCount == 0);
	// This will force garbage collection of #lpSock to occur if it
	// exists, which will call freeSocket() to close and dispose
	self->Remove(_T("#lpOut"));
	return VALUE();
}

#endif #if !defined(WIN32_PLATFORM_PSPC)

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

#ifndef DIALECT_LITE

CMetaOutImpl::CMetaOutImpl():CClassImpl(VALUE(), _T("screen"))
{
	m_MemberSymTab = AllocFrame(19);
	// Anything added here is static (becomes shared by all instances of the class)
	m_MemberSymTab.Set(_T("__type__"), _T("screen"));
	m_MemberSymTab.Set(_T("__init__"), AllocFunction(__init__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("__del__"), AllocFunction(__del__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("write"), AllocFunction(write, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("writeline"), AllocFunction(writeline, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("tab"), AllocFunction(writeTab, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("table"), AllocFunction(writeTable, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("sectitem"), AllocFunction(writeSitem, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startsection"), AllocFunction(startSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("endsection"), AllocFunction(endSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("newpage"), AllocFunction(newPage, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startdoc"), AllocFunction(startDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("enddoc"), AllocFunction(endDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("abortdoc"), AllocFunction(abortDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("linesleft"), AllocFunction(linesLeft, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("textwidth"), AllocFunction(textWidth, (LPCTSTR)OUTPUT_MODULE_NAME));

	// Set margins to default of 1 inch on all sides
	VALUE vMargins = AllocArray(4);
	vMargins.Set(1, 1.0f);
	vMargins.Set(2, 1.0f);
	vMargins.Set(3, 1.0f);
	vMargins.Set(4, 1.0f);
	m_MemberSymTab.Set(_T("margins"), AllocProperty(AllocNil(),
		AllocFunction(writeMargins, (LPCTSTR)OUTPUT_MODULE_NAME), vMargins));

	m_MemberSymTab.Set(_T("pageheaders"), AllocProperty(AllocNil(),
		AllocFunction(writePageHeaders, (LPCTSTR)OUTPUT_MODULE_NAME), AllocArray(0)));

	m_MemberSymTab.Set(_T("wrapindent"), AllocProperty(AllocNil(),
		AllocFunction(writeWrapIndent, (LPCTSTR)OUTPUT_MODULE_NAME), (int)0));

	m_MemberSymTab.Set(_T("position"),
		AllocProperty(AllocFunction(readPosition, (LPCTSTR)OUTPUT_MODULE_NAME),
		AllocFunction(writePosition, (LPCTSTR)OUTPUT_MODULE_NAME), (int)0));

	m_MemberSymTab.Set(_T("font"), AllocProperty(AllocNil(),
		AllocFunction(writeFont, (LPCTSTR)OUTPUT_MODULE_NAME), AllocNil()));
}

CMetaOutImpl::~CMetaOutImpl()
{
}

VALUE CMetaOutImpl::__init__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("outputs.screen.__init__ requires zero args"));
		return AllocNil();
	}
	else {
		CMetaOutputDev* Out = new CMetaOutputDev(pInterp);
		Out->ShowWindow();
		self->Set(_T("#lpOut"), AllocVoid(Out, freeOutputDev, _T("cmetaoutputdev")));
	}

	return VALUE();
}

VALUE CMetaOutImpl::__del__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// There shouldn't be any args for the destructor
	ASSERT(argCount == 0);
	// This will force garbage collection of #lpSock to occur if it
	// exists, which will call freeSocket() to close and dispose
	self->Remove(_T("#lpOut"));
	return VALUE();
}

#endif // #ifndef DIALECT_LITE

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

CRTFOutImpl::CRTFOutImpl():CClassImpl(VALUE(), _T("rtf"))
{
	m_MemberSymTab = AllocFrame(19);
	// Anything added here is static (becomes shared by all instances of the class)
	m_MemberSymTab.Set(_T("__type__"), _T("rtf"));
	m_MemberSymTab.Set(_T("__init__"), AllocFunction(__init__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("__del__"), AllocFunction(__del__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("write"), AllocFunction(write, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("writeline"), AllocFunction(writeline, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("tab"), AllocFunction(writeTab, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("table"), AllocFunction(writeTable, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("sectitem"), AllocFunction(writeSitem, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startsection"), AllocFunction(startSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("endsection"), AllocFunction(endSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("newpage"), AllocFunction(newPage, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startdoc"), AllocFunction(startDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("enddoc"), AllocFunction(endDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("abortdoc"), AllocFunction(abortDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("linesleft"), AllocFunction(linesLeft, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("textwidth"), AllocFunction(textWidth, (LPCTSTR)OUTPUT_MODULE_NAME));

	// Set margins to default of 1 inch on all sides
	VALUE vMargins = AllocArray(4);
	vMargins.Set(1, 1.0f);
	vMargins.Set(2, 1.0f);
	vMargins.Set(3, 1.0f);
	vMargins.Set(4, 1.0f);
	m_MemberSymTab.Set(_T("margins"), AllocProperty(AllocNil(),
		AllocFunction(writeMargins, (LPCTSTR)OUTPUT_MODULE_NAME), vMargins));

	m_MemberSymTab.Set(_T("pageheaders"), AllocProperty(AllocNil(),
		AllocFunction(writePageHeaders, (LPCTSTR)OUTPUT_MODULE_NAME), AllocArray(0)));

	m_MemberSymTab.Set(_T("wrapindent"), AllocProperty(AllocNil(),
		AllocFunction(writeWrapIndent, (LPCTSTR)OUTPUT_MODULE_NAME), (int)0));
}

CRTFOutImpl::~CRTFOutImpl()
{
}

VALUE CRTFOutImpl::__init__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("rtfOut.__init__ requires takes zero args"));
		return AllocNil();
	}
	else {
		CRTFOutputDev* lpOut = new CRTFOutputDev();
		self->Set(_T("#lpOut"), AllocVoid(lpOut, freeOutputDev, _T("crtfoutputdev")));
	}

	return VALUE();
}

VALUE CRTFOutImpl::__del__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// There shouldn't be any args for the destructor
	ASSERT(argCount == 0);
	// This will force garbage collection of #lpSock to occur if it
	// exists, which will call freeSocket() to close and dispose
	self->Remove(_T("#lpOut"));
	return VALUE();
}

////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

CWindowOutImpl::CWindowOutImpl():CClassImpl(VALUE(), _T("window"))
{
	m_MemberSymTab = AllocFrame(19);
	// Anything added here is static (becomes shared by all instances of the class)
	m_MemberSymTab.Set(_T("__type__"), _T("window"));
	m_MemberSymTab.Set(_T("__init__"), AllocFunction(__init__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("__del__"), AllocFunction(__del__, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("write"), AllocFunction(write, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("writeline"), AllocFunction(writeline, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("tab"), AllocFunction(writeTab, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("table"), AllocFunction(writeTable, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("sectitem"), AllocFunction(writeSitem, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startsection"), AllocFunction(startSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("endsection"), AllocFunction(endSection, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("newpage"), AllocFunction(newPage, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("startdoc"), AllocFunction(startDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("enddoc"), AllocFunction(endDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("abortdoc"), AllocFunction(abortDoc, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("linesleft"), AllocFunction(linesLeft, (LPCTSTR)OUTPUT_MODULE_NAME));
	m_MemberSymTab.Set(_T("textwidth"), AllocFunction(textWidth, (LPCTSTR)OUTPUT_MODULE_NAME));

	// Set margins to default of 1 inch on all sides
	VALUE vMargins = AllocArray(4);
	vMargins.Set(1, 1.0f);
	vMargins.Set(2, 1.0f);
	vMargins.Set(3, 1.0f);
	vMargins.Set(4, 1.0f);
	m_MemberSymTab.Set(_T("margins"), AllocProperty(AllocNil(),
		AllocFunction(writeMargins, (LPCTSTR)OUTPUT_MODULE_NAME), vMargins));

	m_MemberSymTab.Set(_T("pageheaders"), AllocProperty(AllocNil(),
		AllocFunction(writePageHeaders, (LPCTSTR)OUTPUT_MODULE_NAME), AllocArray(0)));

	m_MemberSymTab.Set(_T("wrapindent"), AllocProperty(AllocNil(),
		AllocFunction(writeWrapIndent, (LPCTSTR)OUTPUT_MODULE_NAME), (int)0));
}

CWindowOutImpl::~CWindowOutImpl()
{
}

VALUE CWindowOutImpl::__init__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	if (argCount != 0) {
		pInterp->SyntaxError(_T("outputmod.window.__init__ requires takes zero args"));
		return AllocNil();
	}
	else {
		CEditOutputDev* out = new CEditOutputDev(pInterp);
		out->ShowWindow();			// If it's invisible
		self->Set(_T("#lpOut"), AllocVoid(out, freeOutputDev, _T("ceditoutputdev")));
	}

	return VALUE();
}

VALUE CWindowOutImpl::__del__(VALUE* self, int argCount, VALUE args[], CInterpreter* pInterp)
{
	ASSERT_SELF((LPCTSTR)OUTPUT_MODULE_NAME);

	// There shouldn't be any args for the destructor
	ASSERT(argCount == 0);
	// This will force garbage collection of #lpSock to occur if it
	// exists, which will call freeSocket() to close and dispose
	self->Remove(_T("#lpOut"));
	return VALUE();
}
